/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.bid.service.plugins  
 */
package com.apache.bid.service.plugins;

import java.util.List;

import org.apache.log4j.Logger;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.entity.BidApply;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;

/**
 * description:  查询竞买人申请表实体或列表
 * @author mhguo 创建时间：2016-4-12  
 * 
 */
public class BidApplyPlugin implements PluginConnector {
	private IDao bidApplyDao;
	private Logger logger = Logger.getLogger(BidApplyPlugin.class);

	/**
	 * TODO 获取竞买人申请表实体或列表插件<br>  
	 * @see com.zbxsoft.api.manager.PluginConnector#execute(com.zbxsoft.api.vo.ParamsVo)  
	 */
	@Override
	public Object execute(ParamsVo vo) throws Exception {
		ResultEntity result = new ResultEntity();

		try {
			result.setResult("true");
			String userName = String.valueOf(vo.getParams("userName"));
			String userId = String.valueOf(vo.getParams("userId"));
			String applyId = String.valueOf(vo.getParams("applyId"));
			String sessionId = String.valueOf(vo.getParams("sessionId"));
			String itemId = String.valueOf(vo.getParams("itemId"));

			if (Validator.isNull(userName) && Validator.isNull(userId) && Validator.isNull(applyId)) {
				result.setResult("false");
				result.setMessage("查询条件为空！");
				return result;
			}
			MethodParam param = null;
			if (Validator.isNotNull(applyId)) {
				param = new MethodParam("ById", "", "", BidApply.class.getName());
				param.setInfoId(applyId);

				BidApply apply = (BidApply) bidApplyDao.selectById(param);
				if (!Validator.isEmpty(apply)) {
					result.setEntity(apply);
					result.setMessage("查询单个apply对象成功！");
				} else {
					result.setMessage("查询单个apply对象失败！");
					result.setResult("false");
				}
				return result;
			}

			param = new MethodParam("ByProperty", "", "", BidApply.class.getName());
			param.setResultCache(false);
			if (Validator.isNotNull(userId)) {
				param.setParams("userId", userId);
			} else if (Validator.isNotNull(userName)) {
				param.setParams("userName", " = '" + userName + "' ");
			}
			if (Validator.isNotNull(itemId) && !"0".equals(itemId)) {
				param.setParams("itemId", itemId);
			} else if (Validator.isNotNull(sessionId)) {
				param.setParams("sessionId", sessionId);
			}

			int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "0"));
			int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "0"));
			param.setResultCache(false);
			String orderBy = Validator.getDefaultStr(String.valueOf(vo.getParams("orderBy")),
					" order by create_time desc ");
			param.setParams("orderBy", orderBy);
			logger.info("执行查询条件：" + param.getParams());
			if (pageSize == 0 && pageIndex == 0) {
				List list = bidApplyDao.select(param);
				result.setEntity(list);
			} else {
				param.setPageIndex(pageIndex);
				param.setPageSize(pageSize);
				Page page = bidApplyDao.pageSelect(param);
				result.setEntity(page);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setResult("false");
			logger.error("执行BidApply查询失败 ", e);
			result.setMessage("BidApply查询失败！");
		}
		return result;
	}

	public void setBidApplyDao(IDao bidApplyDao) {
		this.bidApplyDao = bidApplyDao;
	}

}
